'use strict';

const { Controller } = require('egg');
const { Op } = require('sequelize');
const serviceName = 'post'
class RouterController extends Controller {
    async index() {
        const { query, service } = this.ctx;
        const { pageNum, pageSize, user_id, category, post_category_id, post_category,address, day, dance,keyword} = query;

        let page = Number(pageNum) || 1;
        let size = Number(pageSize) || 10;
        let where = {}
        if (user_id) {
            where.user_id = user_id
        }
        if (keyword) {
            // name desc
            where[Op.or] = [
                {
                    name: {
                        [Op.like]: `%${keyword}%`
                    }
                },
                {
                    desc: {
                        [Op.like]: `%${keyword}%`
                    }
                }
            ]
        }
        if (category) {
            where.category = category
        }
        if (post_category) {
            where.post_category = post_category
        }
        if (post_category_id) {
            where.post_category_id = post_category_id
        }
        if (address) {
            // like
            where.address = {
                [Op.like]: `%${address}%`
            }
        }
        const list = await service[serviceName].getPage(where, page, size);
        this.ctx.success(list);
    }
    async allList() {
        const { service } = this.ctx;
        const { query } = this.ctx;
        const {
            keyword,
            user_id,
            category,
            post_category_id,
            post_category,
            address,
            dance,
            day,
            startDate,
            endDate,
            startTime,
            endTime,
            week= '',
            startDate2,
            endDate2,
        } = query;
        let where = {}
        if (user_id) {
            where.user_id = user_id
        }
        if (keyword) {
            // name desc
            where[Op.or] = [
                {
                    name: {
                        [Op.like]: `%${keyword}%`
                    }
                },
                {
                    desc: {
                        [Op.like]: `%${keyword}%`
                    }
                }
            ]
        }
        if (category) {
            where.category = category
        }
        if (post_category) {
            where.post_category = post_category
        }
        if (post_category_id) {
            where.post_category_id = post_category_id
        }
        if (address) {
            // like
            where.address = {
                [Op.like]: `%${address}%`
            }
        }
        if (day) {
            // 小于等于day
            where.day = {
                [Op.lte]: day
            }
        }
        if (dance) {
            where.dance = {
                [Op.lte]: dance
            }
        }
        // 关于时间
        if (category === '兼职岗') {
            // 当天的数据
            where.end_time = {
                [Op.gte]: endDate
            }
        }
        if (category === '时段岗') {
            // start_time end_time week
            // 大于开始 结束时间 并且在星期几 开始、结束时间为 小时：分
            if (startTime) {
                // 数据库时间为22:51，我传入时间为22:54，期望查出来
               // where start_time >= startTime
                // where end_time <= endTime
                where.start_time = {
                    [Op.lte]: startTime
                }
            }
            if (endTime) {
                where.end_time = {
                    [Op.gte]: endTime
                }
            }
            // week是个数字join的字符串，比如1，3 数据库为1，2，3，4，5，6，7，这样查询是包含关系
            if (week) {
                let weekList = week.split(',')
                // 多个like，week WHERE week LIKE '%1%' OR week LIKE '%5%';
                where.week = {
                    [Op.or]: weekList.map(item => {
                        return {
                            [Op.like]: `%${item}%`
                        }
                    })
                }
            }
        }
        if (category === '短期岗') {
            // startDate2 endDate2
            if (startDate2) {
                // start_date小于等于startDate2
                where.start_date = {
                    [Op.lte]: new Date(startDate2)
                }
            }
            if (endDate2) {
                where.end_date = {
                    [Op.gte]: new Date(endDate2)
                }
            }
            console.log(where)
        }
        const list = await service[serviceName].allList(where);
        this.ctx.success(list);
    }
    async create() {
        const { ctx } = this;
        const data = ctx.request.body;
        const res = await ctx.service[serviceName].create(data);
        ctx.success(res, 200);
    }
    async update() {
        const { ctx } = this;
        const id = ctx.params.id;
        const infos = ctx.request.body;
        if (!id) throw new Error('必须传递ID参数');
        if (Object.keys(infos).length === 0) throw new Error('请传递修改的内容');
        await ctx.service[serviceName].update(id, infos);
        const res = await ctx.service[serviceName].findOne(id);
        ctx.success(res, 200);
    }
    async sort() {
        const { ctx } = this;
        const { id } = ctx.request.body;
        if (!id) throw new Error('必须传递ID参数');
        await ctx.service[serviceName].sort(id);
        ctx.success(id, 200);
    }
    async destroy() {
        const uid = Number(this.ctx.params.id);
        if (!uid) throw new Error('ID 有误');
        await this.ctx.service[serviceName].deleteById(uid);
        this.ctx.success(uid); // 删除成功返回被删除的用户ID
    }
    async show() {
        const { params, service } = this.ctx;
        const findItem = await service[serviceName].findOne(params.id);
        this.ctx.success(findItem);
    }
}

module.exports = RouterController;
